# 人脸检测系统单元测试运行脚本 (Windows PowerShell)
# 作者: fsserver
# 日期: 2024-01-01

Write-Host "==========================================" -ForegroundColor Green
Write-Host "人脸检测系统单元测试开始" -ForegroundColor Green
Write-Host "==========================================" -ForegroundColor Green

# 设置环境变量
$env:MAVEN_OPTS = "-Xmx2g -XX:MaxPermSize=256m"

# 清理之前的构建
Write-Host "清理之前的构建..." -ForegroundColor Yellow
mvn clean

if ($LASTEXITCODE -ne 0) {
    Write-Host "清理失败！" -ForegroundColor Red
    exit 1
}

# 编译项目
Write-Host "编译项目..." -ForegroundColor Yellow
mvn compile test-compile

if ($LASTEXITCODE -ne 0) {
    Write-Host "编译失败！" -ForegroundColor Red
    exit 1
}

# 运行单元测试
Write-Host "运行单元测试..." -ForegroundColor Yellow
mvn test

if ($LASTEXITCODE -eq 0) {
    Write-Host "==========================================" -ForegroundColor Green
    Write-Host "所有单元测试通过！" -ForegroundColor Green
    Write-Host "==========================================" -ForegroundColor Green
    
    # 生成测试报告
    Write-Host "生成测试报告..." -ForegroundColor Yellow
    mvn surefire-report:report
    
    # 生成代码覆盖率报告
    Write-Host "生成代码覆盖率报告..." -ForegroundColor Yellow
    mvn jacoco:report
    
    Write-Host "测试报告生成完成：" -ForegroundColor Green
    Write-Host "- 单元测试报告: target/site/surefire-report.html" -ForegroundColor Cyan
    Write-Host "- 代码覆盖率报告: target/site/jacoco/index.html" -ForegroundColor Cyan
    
} else {
    Write-Host "==========================================" -ForegroundColor Red
    Write-Host "单元测试失败！请检查测试结果。" -ForegroundColor Red
    Write-Host "==========================================" -ForegroundColor Red
    
    # 即使测试失败也生成报告
    Write-Host "生成测试报告..." -ForegroundColor Yellow
    mvn surefire-report:report
    
    Write-Host "测试报告已生成: target/site/surefire-report.html" -ForegroundColor Cyan
}

# 运行集成测试
Write-Host "运行集成测试..." -ForegroundColor Yellow
mvn verify -Dskip.unit.tests=true

if ($LASTEXITCODE -eq 0) {
    Write-Host "==========================================" -ForegroundColor Green
    Write-Host "所有集成测试通过！" -ForegroundColor Green
    Write-Host "==========================================" -ForegroundColor Green
} else {
    Write-Host "==========================================" -ForegroundColor Red
    Write-Host "集成测试失败！请检查测试结果。" -ForegroundColor Red
    Write-Host "==========================================" -ForegroundColor Red
}

Write-Host "==========================================" -ForegroundColor Green
Write-Host "所有测试完成！" -ForegroundColor Green
Write-Host "==========================================" -ForegroundColor Green

# 显示测试结果文件位置
if (Test-Path "target/site/surefire-report.html") {
    Write-Host "测试报告文件:" -ForegroundColor Yellow
    Write-Host "  - HTML报告: $(Resolve-Path 'target/site/surefire-report.html')" -ForegroundColor Cyan
}

if (Test-Path "target/site/jacoco/index.html") {
    Write-Host "  - 覆盖率报告: $(Resolve-Path 'target/site/jacoco/index.html')" -ForegroundColor Cyan
}

if (Test-Path "target/surefire-reports") {
    Write-Host "  - 详细报告目录: $(Resolve-Path 'target/surefire-reports')" -ForegroundColor Cyan
} 